const loginDao = require('../dao/loginDao');
const serviceCommon = require('../util/serviceCommon');
const commonUtil = require('../util/commonUtils');
const MD5 = require('md5');

const service = {
    handleLogin: async (data) => {
        data.password = MD5(data.password + "linglan");
        return new Promise((resolve, reject) => {
            return serviceCommon.login(loginDao, 'handleLogin', data).then((result) => {
                if (result.code === 1) {
                    let param = {
                        token: commonUtil.genUUID(),
                        username: data.username
                    };
                    return loginDao.handleAddToken(param).then((result1) => {
                        if (result1 > 0) {
                            result.token = param.token;
                            resolve(result);
                        }
                    }).catch((error) => {
                        let data = {code: 3, msg: error.message};
                        reject(data);
                    })
                } else {
                    reject(result);
                }
            }, (err) => {
                reject(err);
            }).catch((error) => {
                let data = {code: 3, msg: error.message};
                reject(data);
            })
        });
    },
    handleAddAccount: async (data) => {
        data.password = MD5(data.password + "linglan");
        return new Promise((resolve, reject) => {
            //通过用户名查找是否有重复的名字
            return loginDao.searchAdminByName(data).then((admin) => {
                if (admin.length > 0) {
                    let data = {code: 2, msg: '账号名重复'};
                    reject(data);
                } else {
                    return serviceCommon.addDataMethod(loginDao, "handleAddAccount", data).then((admin1) => {
                        resolve(admin1);
                    }, (err) => {
                        reject(err);
                    }).catch((error) => {
                        reject(error);
                    })
                }
            }).catch((error) => {
                let data = {code: 3, msg: error.message};
                reject(data);
            })
        })
    },
    handleEditAccount: async (data) => {
        data.password = MD5(data.password + "linglan");
        let result;
        result = await serviceCommon.editDataMethod(loginDao, 'handleEditAccount', data);
        return result;
    },
    handleDeleteAccount: async (data) => {
        let result;
        result = await serviceCommon.deleteDataMethod(loginDao, 'handleDeleteAccount', data);
        return result;
    },
    handleGetAccountList: async () => {
        let result;
        result = await serviceCommon.getListMethod(loginDao, 'handleGetAccountList');
        if (result.data.length > 0) {
            for (let i = 0; i < result.data.length; i++) {
                result.data[i].number = i + 1;
            }
        }
        return result;
    }
};
module.exports = service;